package alan.leetcode.demo.thread.no1114;

import org.junit.Test;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 1114 按序打印
 * <p>
 * 我们提供了一个类：
 * <p>
 * public class Foo {
 *   public void first() { print("first"); }
 *   public void second() { print("second"); }
 *   public void third() { print("third"); }
 * }
 * 三个不同的线程 A、B、C 将会共用一个 Foo 实例。
 * <p>
 * 一个将会调用 first() 方法
 * 一个将会调用 second() 方法
 * 还有一个将会调用 third() 方法
 * 请设计修改程序，以确保 second() 方法在 first() 方法之后被执行，third() 方法在 second() 方法之后被执行。
 * <p>
 *  
 * <p>
 * 示例 1:
 * <p>
 * 输入: [1,2,3]
 * 输出: "firstsecondthird"
 * 解释:
 * 有三个线程会被异步启动。
 * 输入 [1,2,3] 表示线程 A 将会调用 first() 方法，线程 B 将会调用 second() 方法，线程 C 将会调用 third() 方法。
 * 正确的输出是 "firstsecondthird"。
 * 示例 2:
 * <p>
 * 输入: [1,3,2]
 * 输出: "firstsecondthird"
 * 解释:
 * 输入 [1,3,2] 表示线程 A 将会调用 first() 方法，线程 B 将会调用 third() 方法，线程 C 将会调用 second() 方法。
 * 正确的输出是 "firstsecondthird"。
 *  
 * <p>
 * 提示：
 * <p>
 * 尽管输入中的数字似乎暗示了顺序，但是我们并不保证线程在操作系统中的调度顺序。
 * 你看到的输入格式主要是为了确保测试的全面性。
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/print-in-order
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 *
 * @author: Alan
 * @date: 2021/2/21 15:22
 */
public class Thread1114 {

    int num = 1;

    Lock lock;
    Condition condition1;
    Condition condition2;
    Condition condition3;

    public Thread1114(){
        num = 1;
        lock = new ReentrantLock();;
        condition1 = lock.newCondition();
        condition2 = lock.newCondition();
        condition3 = lock.newCondition();

    }

    public void first() {
        lock.lock();
        try {
            while (num != 1){
                condition1.await();
            }
            num = 2;
            System.out.println("first");
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void second() {
        lock.lock();
        try {
            while (num != 2){
                condition2.await();
            }
            num = 3;
            System.out.println("second");
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void third() {
        lock.lock();
        try {
            while (num != 3){
                condition3.await();
            }
            num = 1;
            System.out.println("third");
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public Thread getThread(String method,Thread1114 thread1114){
        Thread thread = null;
        Runnable r = null;
        switch (method){
            case "A":
                r = ()->{
                    thread1114.first();
                };
                break;
            case "B":
                r = ()->{
                    thread1114.second();
                };
                break;
            case "C":
                r = ()->{
                    thread1114.third();
                };
                break;
        }
        thread = new Thread(r);
        return thread;
    }

    @Test
    public void test1114() throws InterruptedException {
        Thread1114 thread1114 = new Thread1114();
        Thread A = thread1114.getThread("A",thread1114);
        Thread A2 = thread1114.getThread("A",thread1114);
        Thread B = thread1114.getThread("B",thread1114);
        Thread B2 = thread1114.getThread("B",thread1114);
        Thread C = thread1114.getThread("C",thread1114);
        Thread C2 = thread1114.getThread("C",thread1114);
        A.start();
        A2.start();
        B.start();
        B2.start();
        C.start();
        C2.start();
        Thread.sleep(2000);
    }


}
